//转意符转符合
function html_decode(str) 
{ 
    var s = ""; 
    if (str.length == 0) return ""; 
    s = str.replace(/&amp;/g, "&"); 
    s = s.replace(/&lt;/g, "<"); 
    s = s.replace(/&gt;/g, ">"); 
    s = s.replace(/&nbsp;/g, " "); 
    s = s.replace(/&#39;/g, "\'"); 
    s = s.replace(/&quot;/g, "\""); 
    s = s.replace(/<br\/>/g, "\n"); 
    return s; 
} 

//回到顶部开始
//$("#scroll-up").fadeOut()
$(window).scroll(function () {
		if ($(this).scrollTop() > 1) {
				$("#scroll-up").fadeIn();
			} else {
				$("#scroll-up").fadeOut();
			}
	});
$("#scroll-up").click(function () {
		if ($(window).scrollTop() > 0) {
			$("html,body").stop().animate({ scrollTop: 0 }, 600);
		}
});
//回到顶部结束

//字典回显
function selectDictLabel(datas, value) {
	var actions = [];
    $.each(datas, function(index, dict) {
        if (dict.dictValue == ('' + value)) {
        	var listClass = commonequals("default", dict.listClass) || commonisEmpty(dict.listClass) ? "" : "badge badge-" + dict.listClass;
        	actions.push(commonsprintf("<span class='%s'>%s</span>", listClass, dict.dictLabel));
            return false;
        }
    });
    return actions.join('');
}

function commonsprintf(str) {
    var args = arguments, flag = true, i = 1;
    str = str.replace(/%s/g, function () {
        var arg = args[i++];
        if (typeof arg === 'undefined') {
            flag = false;
            return '';
        }
        return arg;
    });
    return flag ? str : '';
}

function commonequals(str, that) {
	return str == that;
}

function commonisEmpty(value) {
    if (value == null) {
        return true;
    }
    return false;
}

$.cookie = function(name, value, options) {if (typeof value != 'undefined') {
	    options = options || {};
	    if (value === null) {
	              value = '';
	              options = $.extend({}, options);
	              options.expires = -1;
	    }
	    var expires = '';
	    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
	              var date;
	              if (typeof options.expires == 'number') {
	                        date = new Date();
	                        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
	              } else {
	                        date = options.expires;
	              }
	              expires = '; expires=' + date.toUTCString();
	    }
	    var path = options.path ? '; path=' + (options.path) : '';
	    var domain = options.domain ? '; domain=' + (options.domain) : '';
	    var secure = options.secure ? '; secure' : '';
	    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	    return this;
	} else {
	    var cookieValue = null;
	    if (document.cookie && document.cookie != '') {
	              var cookies = document.cookie.split(';');
	              for (var i = 0; i < cookies.length; i++) {
	                    var cookie = cookies[i].trim();
	                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
	                              cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                              break;
	                    }
	              }
	    }
	    return cookieValue;
	}
};

function honeyAjax(obj){
	var url = obj;
    $.ajax({
        type: 'GET',
        url: url,
        dataType: "json",
        success: function (data) {
        	layer.msg("成功")
        },
        error: function (xhr, errorText, errorType) {        //xhr:XMLHttpRequest对象  errorText:错误信息  erroType：（可选）捕获的异常对象
            alert('错误');    //自定义错误
        }
    });
}

var defaultimg = "'/img/loading.gif'";

//加载main页blog内容
function loadingBlog(url,obj,flag) {
	document.getElementById("nextpage").style.display = "block";
	var myajax = $.ajax({
			url : url + obj,
			method : 'get',
			dataType : 'json',
			success : function(data) {
				var rows = data.rows;
				total = data.total;
				var htmlText = "";
				if(flag==2 && total==0){
					htmlText += '<div class="post-preview text-center">';
					htmlText += '<span class="post-title" style="font-size:40px;">暂时没有搜索到任何内容！你可以换个关键词试试哦。</span><br/>';
					htmlText += '<img src="/img/logo/honey-b.png" alt="加载中">';
					htmlText += '</div>';
				}else{
					if(total==0){
						htmlText += '<div class="post-preview text-center">';
						htmlText += '<span class="post-title" style="font-size:40px;">暂时没有任何内容哦!</span><br/>';
						htmlText += '<img src="/img/logo/honey-b.png" alt="">';
						htmlText += '</div>';
					}else{
						for (i = 0; i < rows.length; i++) {
							htmlText += '<div class="col-md-4 col-xs-12 column" style="margin-top:20px;height:330px;background-color:rgba(0,0,0,0);">';
							htmlText += '<div class="col-md-12 text-center" style="background-color:rgba(0,0,0,0);padding-top: 10px;border-radius:15px 50px;box-shadow: 10px 10px 5px #333333;border:1px solid #E6E6E6">';
							htmlText += '<a value="' + rows[i].contentId + '" onclick="opencontent(this)" >';
							if(rows[i].contentId%5==0){
								htmlText += '<img title="点击查看详情" class="img-rounded" src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2002984081,2728237884&fm=11&gp=0.jpg" onerror="this.src='+defaultimg+'" id="randimg" alt="如长时间未加载出请刷新试试" height="auto" width="70%" style="cursor:pointer;">';
							}else if(rows[i].contentId%5==1){
								htmlText += '<img title="点击查看详情" class="img-rounded" src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2682704700,3868252297&fm=26&gp=0.jpg" onerror="this.src='+defaultimg+'" id="randimg" alt="如长时间未加载出请刷新试试" height="auto" width="70%" style="cursor:pointer;">';
							}else if(rows[i].contentId%5==2){
								htmlText += '<img title="点击查看详情" class="img-rounded" src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2156416487,1356493703&fm=26&gp=0.jpg" onerror="this.src='+defaultimg+'" id="randimg" alt="如长时间未加载出请刷新试试" height="auto" width="70%" style="cursor:pointer;">';
							}else if(rows[i].contentId%5==3){
								htmlText += '<img title="点击查看详情" class="img-rounded" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=823809738,973193450&fm=11&gp=0.jpg" onerror="this.src='+defaultimg+'" id="randimg" alt="如长时间未加载出请刷新试试" height="auto" width="70%" style="cursor:pointer;">';
							}else if(rows[i].contentId%5==4){
								htmlText += '<img title="点击查看详情" class="img-rounded" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1306607258,967818222&fm=26&gp=0.jpg" onerror="this.src='+defaultimg+'" id="randimg" alt="如长时间未加载出请刷新试试" height="auto" width="70%" style="cursor:pointer;">';
							}
							htmlText += '</a>';
							htmlText += '<a class="contenttitle hand adt textdanlan" title="'+rows[i].title+'" value="' + rows[i].contentId + '" onclick="opencontent(this)" style="text-decoration:none;font-size:18px;font-weight:bold;">&nbsp;';
							htmlText += rows[i].title;
							htmlText += '</a>';
							htmlText += '<span class="hand fontsize12">作者：<a class="textdanlan" value="' + rows[i].createdId + '" onclick="openuser(this)">'
							+ rows[i].author + '</a></span>&emsp;<span class="hand fontsize12 textdanlan">创建时间:'+ rows[i].createDate +'</span><br><br>';	
							htmlText += '</div>';
							htmlText += '</div>';
						}
						var maxtotal;
						if(flag==0){
							maxtotal=obj * pagesize;
						}else if(flag==1){
							maxtotal=obj * pagesize1
						}else if(flag==2){
							maxtotal=obj * pagesize2
						}
						//alert(obj)
						//alert(maxtotal)
						//alert(total)
						if (total <= maxtotal) {
							document.getElementById("allcontent").style.display = "block";
							document.getElementById("morecontent").style.display = "none";
						}else{
							document.getElementById("allcontent").style.display = "none";
							document.getElementById("morecontent").style.display = "block";
						}
						if(flag==0){
							pagenum++;
						}else if(flag==1){
							pagenum1++;
						}else if(flag==2){
							pagenum2++;
						}
					}
				}
				$("#mainContentChild").append(htmlText);
			}
		});
	//ajax执行完执行
	$.when(myajax).done(function () {
			layer.msg("内容加载完毕!");
      }); 
}

//加载ta人blog内容
function loadingBlog1(url,obj) {
	document.getElementById("nextpage").style.display = "block";
	var myajax = $.ajax({
		url : url + obj,
		method : 'get',
		dataType : 'json',
		success : function(data) {
			var rows = data.rows;
			total = data.total;
			var htmlText = "";
			for (i = 0; i < rows.length; i++) {
				htmlText += '<div class="col-md-12 col-xs-12 column" style="margin-top:20px;height:150px;background-color:rgba(0,0,0,0);">';
				htmlText += '<div class="col-md-1"></div>'
				htmlText += '<div class="col-md-10  col-xs-12" style="background-color:rgba(0,0,0,0);padding-top: 10px;">';
				htmlText += '<a class="contenttitle hand textdanlan" title="'+rows[i].title+'" value="' + rows[i].contentId + '" onclick="opencontent(this)" style="text-decoration:none;font-size:30px;font-weight:bold;">&nbsp;<span>';
				htmlText += rows[i].title;
				htmlText += '</span></a><br>';
				htmlText += '<span class="hand adt fontsize12 pull-right textdanlan">发布时间:'+ rows[i].createDate +'</span><br><hr>';	
				htmlText += '</div>';
				htmlText += '<div class="col-md-1"></div>'
				htmlText += '</div>';
				
			}
			$("#mainContentChild").append(htmlText);
			if (total <= obj * pagesize) {
				document.getElementById("allcontent").style.display = "block";
				document.getElementById("morecontent").style.display = "none";
			}else{
				document.getElementById("allcontent").style.display = "none";
				document.getElementById("morecontent").style.display = "block";
			}
			pagenum++;
		}
	});
	//ajax执行完执行
	$.when(myajax).done(function () {
		layer.msg("内容加载完毕!");
	}); 
}

//加载ta人blog内容
function loadingBlog2(url,obj,mdiv,all,more,num) {
	document.getElementById("nextpage").style.display = "block";
	var myajax = $.ajax({
		url : url + obj,
		method : 'get',
		dataType : 'json',
		success : function(data) {
			var rows = data.rows;
			total = data.total;
			var htmlText = "";
			if(rows.length < 1){
				htmlText += '<div class="col-md-12 col-xs-12 column text-center" style="margin-top:20px;height:360px;background-color:rgba(0,0,0,0);">';
				htmlText += '<img alt="如长时间未加载请刷新试试" class="img-circle xzyz" height="300" width="300" title="去ta主页看看..." src="/img/logo/honey-b.png" onerror="/img/logo/honey-b.png" >';
				htmlText += '<h1 class="texthong">喵!你什么也没留下!!!</h1>';
				htmlText += '</div>';
			}else{
				for (i = 0; i < rows.length; i++) {
					htmlText += '<div class="col-md-12 col-xs-12 column" style="margin-top:20px;height:100px;background-color:rgba(0,0,0,0);">';
					htmlText += '<div class="col-md-12  col-xs-12" style="background-color:rgba(0,0,0,0);padding-top: 20px;border:1px solid blue;border-radius:20px 20px;">';
					if(num==4){
						htmlText += '<b class="contenttitle hand textdanlan" >';
						htmlText += '<img alt="如长时间未加载请刷新试试" class="img-circle xz" height="50" width="50" title="去ta主页看看..." value="' + rows[i].userId + '" onclick="openuser(this)" src="' + rows[i].avatar + '" onerror="/img/logo/honey-b.png" >';
						htmlText += '<span title="去ta主页看看..." value="' + rows[i].userId + '" onclick="openuser(this)" class="fontsize20">' + rows[i].userName + '</span><span class="pull-right">' + rows[i].followers + '</span></b><br>';
				    }else if(num==1){
						htmlText += '<a class="contenttitle hand textdanlan" title="查看评论详情" value="' + rows[i].contentId + '" onclick="opencontent(this)" style="text-decoration:none;font-size:20px;font-weight:bold;">&nbsp;<span>';
						htmlText += rows[i].content;
						htmlText += '</span></a><br>';
						htmlText += '<span class="hand adt fontsize12 pull-right">&emsp;发布时间:'+ rows[i].createDate +'</span><span class="hand adt fontsize12 pull-right" title="删除当前评论" onclick="removecomment(this)" value="' + rows[i].commentId + '">删除</span><br>';	
					}else{
						htmlText += '<a class="contenttitle hand  textdanlan" title="查看帖子详情" value="' + rows[i].contentId + '" onclick="opencontent(this)" style="text-decoration:none;font-size:20px;font-weight:bold;">&nbsp;<span>';
						htmlText += rows[i].title;
						htmlText += '</span></a><br>';
						htmlText += '<span class="hand adt fontsize12 pull-right">发布时间:'+ rows[i].createDate +'</span><br>';	
					}
					htmlText += '</div>';
					htmlText += '</div>';
				}
				var zong;
				if(num==1){
					zong = obj * pagesize1;
				}else if(num==2){
					zong = obj * pagesize2;
				}else if(num==3){
					zong=obj * pagesize3;
				}else if(num==4){
					zong=obj * pagesize4;
				}
				if (total <= zong) {
					$(all).attr("style","display:block");
					$(more).attr("style","display:none");
				}else{
					$(all).attr("style","display:none");
					$(more).attr("style","display:block");
				}
				if(num==1){
					pagenum1++;
				}else if(num==2){
					pagenum2++;
				}else if(num==3){
					pagenum3++;
				}else if(num==4){
					pagenum4++;
				}
			}
			$(mdiv).append(htmlText);
		}
	});
	//ajax执行完执行
	$.when(myajax).done(function () {
		layer.msg("内容加载完毕!");
	}); 
}

//加载相册内容
function loadingImg(url,obj) {
	document.getElementById("nextpage").style.display = "block";
	var myajax = $.ajax({
		url : url + obj,
		method : 'post',
		dataType : 'json',
		success : function(data) {
			var rows = data.rows;
			total = data.total;
			var htmlText = "";
			for (i = 0; i < rows.length; i++) {
				htmlText += '<div class="col-md-3 col-xs-6 column" style="margin-top:20px;height:388px;background-color:rgba(0,0,0,0);">';
				htmlText += '<a href="' + rows[i].fileName + '" data-gallery=""><img class="img-thumbnail" style="cursor:pointer;width:80%" src="' + rows[i].fileName + '"></a>';
				htmlText += '</div>'
			}
			if(rows.length==0){
				htmlText += '<h1>ta还没有上传图片哦</h1>';
			}
			htmlText += '<div id="blueimp-gallery" class="blueimp-gallery">';
			htmlText += '<div class="slides"></div>';
			htmlText += '<h3 class="title"></h3>';
			htmlText += '<a class="prev">‹</a>';
			htmlText += '<a class="next">›</a>';
			htmlText += '<a class="close">×</a>';
			htmlText += '<a class="play-pause"></a>';
			htmlText += '<ol class="indicator"></ol>';
			htmlText += '</div>';
			$("#mainContentChild").append(htmlText);
			if (total <= obj * pagesize) {
				document.getElementById("allcontent").style.display = "block";
				document.getElementById("morecontent").style.display = "none";
			}else{
				document.getElementById("allcontent").style.display = "none";
				document.getElementById("morecontent").style.display = "block";
			}
			pagenum++;
		}
	});
	//ajax执行完执行
	$.when(myajax).done(function () {
		layer.msg("内容加载完毕!");
	}); 
}

//点赞
function zan(){
	$.ajax({
		cache : true,
		type : "post",
		url : "/blog/close/zan",
		data:{cid:$("#cid").text()},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(r) {
			if(r.msg==1){
				parent.layer.msg(r.data);
				//location.reload();
				var p2=parseInt($("#zan").text());
				$("#zan").text(p2+1);
				$("#zan").attr("class","fa fa-thumbs-up");
				document.getElementById("zan").style.color="red";
				//点赞
				$("#dz").attr("class","btn btn-xs btn-danger");
			}else if(r.msg==2){
				parent.layer.msg(r.data);
				//location.reload();
				var p2=parseInt($("#zan").text());
				$("#zan").text(p2-1);
				$("#zan").attr("class","fa fa-thumbs-o-up");
				document.getElementById("zan").style.color="";
				//取消点赞
				$("#dz").attr("class","btn btn-xs btn-success");
			}else{
				parent.layer.msg("你还未登录,请先登录!");
			}
		}
	});
}

//收藏
function favorites(){
	$.ajax({
		cache : true,
		type : "POST",
		url : "/blog/close/favorites",
		data:{cid:$("#cid").text()},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(r) {
			if(r.msg==1){
				parent.layer.msg(r.data);
				var p2=parseInt($("#favorites").text());
				$("#favorites").text(p2+1);
				$("#favorites").attr("class","glyphicon glyphicon-heart");
				document.getElementById("favorites").style.color="red";
				//收藏
				$("#sc").attr("class","btn btn-xs btn-danger");
			}else if(r.msg==2){
				parent.layer.msg(r.data);
				var p2=parseInt($("#favorites").text());
				$("#favorites").text(p2-1);
				$("#favorites").attr("class","glyphicon glyphicon-heart-empty");
				document.getElementById("favorites").style.color="";
				//取消收藏
				$("#sc").attr("class","btn btn-xs btn-success");
			}else{
				parent.layer.msg("你还未登录,请先登录!");
			}
		}
	});
}

//关注
function follow(){
	$.ajax({
		cache : true,
		type : "POST",
		url : "/blog/close/follow",
		data:{uid:$("#userId").text()},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(r) {
			if(r.msg==1){
				parent.layer.msg(r.data);
				var p1=parseInt($("#fans").text());
				$("#fans").text(p1+1);
				document.getElementById("fansspan").style.color="red";
				//$("#fansspan").attr("class","layui-btn layui-btn-radius layui-btn-warm");
				$("#fansspan").css("background-color","#EE11EE");
				$("#fansspan").html("已关注");
			}else if(r.msg==2){
				parent.layer.msg(r.data);
				var p1=parseInt($("#fans").text());
				$("#fans").text(p1-1);
				document.getElementById("fansspan").style.color="";
				$("#fansspan").css("background-color","#A4A4A4");
				$("#fansspan").html("关注");
			}else if(r.msg==3){
				parent.layer.msg(r.data);
			}else{
				parent.layer.msg("你还未登录,请先登录!");
			}
		}
	});
}

//签到
$("#sign").click(function(){
	$.ajax({
		url: "/blog/user/issign",
		type:"POST",
		success:function(r){
			if(r.code=='0'){
				$("#sign").html("已签");
				$("#sign").attr("class","layui-btn layui-btn-warm pull-right");
				$("#sign").attr('id','alreadysign');
				var p=parseInt($("#integrate").text());
				$("#integrate").text(p+1);
				layer.msg(r.msg);
			}else{
				layer.msg(r.msg);
			}
		}
	});
});

function opencontent(obj){
	$(location).prop('href', '/blog/open/post/'+$(obj).attr("value"));
}

function openuser(obj){
	$(location).prop('href', '/blog/open/otherpersonalmain/'+$(obj).attr("value"));
}

function openalbom(obj){
	$(location).prop('href', '/blog/open/albom/'+$(obj).attr("value"));
}

function alertimg(obj){
	layer.open({
		  type: 1,
		  title: false,
		  closeBtn: 0,
		  skin: 'layui-layer-nobg', //没有背景色
		  offset: ['38%', '38%'],//设置位移
		  shadeClose: true,
		  content: '<img height="350" width="350" src="/img/webmaster/'+obj+'" title="微信赞助"/>'
		});
}